// 获取菜单列表接口
import { getMenus } from "../api/layout";
import {useState,useEffect} from 'react';
import {AxiosResponse} from 'axios';
import {UserOutlined,BarChartOutlined,TagOutlined,ShopOutlined,AppstoreOutlined,FileDoneOutlined} from '@ant-design/icons';
// 根据id区分每个菜单图标
let iconList:iconItem = {
    '125':<UserOutlined/>,
    "101":<ShopOutlined/>,
    "103":<TagOutlined/>,
    "102":<FileDoneOutlined/>,
    "145":<BarChartOutlined/>
}
function useMemus(){
    // 存放处理好的菜单列表
    let [itemList,setItemList] = useState<MenuItemType[]>([]);
    // 定义菜单选项
    let memusList:MenuItemType[] = [];
    useEffect(()=>{
        // 获取菜单列表
        getMenus().then((res:AxiosResponse)=>{
            if(res.data.meta.status!==200){
                return
            }
            // 处理菜单列表
            let list:ListItemType[] = res.data.data;
            list.forEach((item:ListItemType)=>{
                let obj:MenuItemType = {
                    icon:iconList[item.id],
                    key:item.id,
                    label:item.authName,
                    children:[],
                    id:item.path,
                }
                if(item.children){
                    item.children.forEach((item1:ListItemType)=>{
                        let obj1:MenuItemType={
                            icon:<AppstoreOutlined/>,
                            key:item1.path,
                            label:item1.authName,
                            id:item1.id
                        }
                        let arry = obj.children as MenuItemType[];
                        arry.push(obj1)
                    })
                }
                memusList.push(obj);
            })
            setItemList(memusList)
        })
    },[])
    return itemList;
}

export default useMemus;